#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<unordered_map>

using namespace std;

int lenLongestFibSubseq(vector<int>& arr) {
    int n = arr.size();
    vector<vector<int>> f(n, vector<int>(n, 2));
    unordered_map<int, int> m;
    for (int i = 0; i < n; i++)    m[arr[i]] = i;

    int res = 0;
    for (int j = 2; j < n; j++)
    {
        for (int i = 1; i < n; i++)
        {
            int a = arr[j] - arr[i];
            if (a < arr[i] && m.count(a)) f[i][j] = f[m[a]][i] + 1;
            res = max(res, f[i][j]);
        }
    }
    return res < 3 ? 0 : res;
}